package com.hualan.servlet;

import com.alibaba.fastjson.JSON;
import com.hualan.dao.CommunityDao;
import com.hualan.dao.impl.CommunityDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/communityDeleteServlet")
public class CommunityDeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        int id = Integer.parseInt(req.getParameter("community_id"));
        System.out.println(id);
        CommunityDao communityDao = new CommunityDaoImpl();
        int row = communityDao.deleteCommunityById(id);
        Map<String,Object> map = new HashMap<>();
        if (row < 1){
            map.put("code", 300);
            map.put("message", "删除失败");
        }else {
            map.put("code", 200);
            map.put("message", "删除成功");
        }
        resp.getWriter().write(JSON.toJSONString(map));

    }
}
